<?php
require(dirname(__FILE__) . '/base.class.php');
class Slider extends Base
{
    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }

    /*
        获取轮播图列表
    */
    function lists()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        // 获取页码
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;
        $sql = "select * from sliders";
        $sql .= " limit {$start},{$page_num}";
        $list = $this->db->query($sql);
        if (!empty($list)) {
            // 将图片路径转换为绝对路径
            foreach ($list as $key => $item) {
                $list[$key]['img'] = path($item['img']);
            }
            // 获取总记录数
            $sql = "select count(id) as count from sliders";
            $res = $this->db->get_one($sql);
            $total = $res['count'];
            $output['data'] = ['list' => $list, 'total' => $total];
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '获取轮播图失败';
        }
        echo json_encode($output);
    }
}
